/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics;

import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;
import tools.Downloader;
import tools.Extractor;
import tools.MessageManager;

/**
 * Classe esattamente identica a ProgressFrame eccetto per l'implementazione di Runnable
 * Classe di prova
 * @author Mattia
 */
public class ProgressFrame extends javax.swing.JFrame implements Runnable, Observer {
 
    public ProgressFrame() {
        
        initComponents();
        this.setLocationRelativeTo(null);
    }
    public void setUp(javax.swing.ImageIcon programIcon, String title, String subTitle) {
        if (!(programIcon == null)) {
            this.ICON_Program.setIcon(programIcon);
        }        
        if (!(title == null || title.equals(""))) {
            this.Title.setText(title);
            this.setTitle(title);
        }
        if (!(subTitle == null || subTitle.equals(""))) {
            this.Subtitles.setText(subTitle);
        }
    }
    
    public void setController(Object o) {
        if(o instanceof Downloader) {
            this.downloader = (Downloader) o;
        } else if(o instanceof Extractor) {
            this.extractor = (Extractor) o;
        }
    }
    
    public void setUp(int maxValue) {
        this.ProgressBar.setMaximum(maxValue);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ProgressBar = new javax.swing.JProgressBar();
        ICON_Download = new javax.swing.JLabel();
        ICON_Program = new javax.swing.JLabel();
        Title = new javax.swing.JLabel();
        Subtitles = new javax.swing.JLabel();
        Action = new javax.swing.JLabel();
        Time = new javax.swing.JLabel();
        BTN_Annulla = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Download Chromium");
        setAlwaysOnTop(true);
        setResizable(false);

        ProgressBar.setStringPainted(true);
        ProgressBar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                ProgressBarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                ProgressBarMouseExited(evt);
            }
        });

        ICON_Download.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/image/Icon_Download.png"))); // NOI18N

        ICON_Program.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/image/Chromium.png"))); // NOI18N

        Title.setFont(new java.awt.Font("Cambria", 1, 18)); // NOI18N
        Title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Title.setText("Chromium Download");

        Subtitles.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Subtitles.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Subtitles.setText("Stiamo scaricando per voi!");

        Action.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        Action.setText("Download in corso ...");
        Action.setFocusable(false);

        Time.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        Time.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        Time.setText("00m 00s 00");

        BTN_Annulla.setText("Annulla");
        BTN_Annulla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_AnnullaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ICON_Download)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Title, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Subtitles, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(Action, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Time, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(ICON_Program, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(BTN_Annulla, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Title, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Subtitles)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Action, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Time, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(ICON_Download)
                    .addComponent(ICON_Program))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(BTN_Annulla, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BTN_AnnullaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_AnnullaActionPerformed
        if(BTN_Annulla.getText().equals("Annulla")) {
            int result = JOptionPane.showConfirmDialog(rootPane, "Vuoi veramente annullare?", "Annulla Download", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(result==0) {
                if(downloader != null) {
                    this.downloader.cancel();
                }
                MessageManager.logger("Annulla il download");
                dispose();
            }
        } else {
            if(downloader != null) {
                this.downloader.cancel();
            }
            dispose();
        }
    }//GEN-LAST:event_BTN_AnnullaActionPerformed

    private void ProgressBarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ProgressBarMouseEntered
        //this.ProgressBar.setStringPainted(false);
    }//GEN-LAST:event_ProgressBarMouseEntered

    private void ProgressBarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ProgressBarMouseExited
        //this.ProgressBar.setStringPainted(true);
    }//GEN-LAST:event_ProgressBarMouseExited

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JLabel Action;
    public javax.swing.JButton BTN_Annulla;
    private javax.swing.JLabel ICON_Download;
    private javax.swing.JLabel ICON_Program;
    public javax.swing.JProgressBar ProgressBar;
    private javax.swing.JLabel Subtitles;
    public javax.swing.JLabel Time;
    private javax.swing.JLabel Title;
    // End of variables declaration//GEN-END:variables
    private Downloader downloader;
    private Extractor extractor;
    
    // Runnable Section
    @Override
    public void run() {
        System.err.println("RST run!!");
        this.setVisible(true);
        System.err.println("RST after setVisible");
    }

    public void end() {
        
    }

    @Override
    public void update(Observable o, Object arg) {
        
    }
    

}
